import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'  //引入Login组件
import Home from '../components/Home.vue'
import Order from '../components/Order.vue'
import Personal from '../components/Personal.vue'
import Shop from '../components/Shop.vue'
import Follow from '../components/Follow.vue'
import shopManage from '../components/shopManage.vue'


Vue.use(VueRouter)

const routes = [
  {
    path:"/",
    redirect:"/login"
  },
  {
    path:"/login",
    component: Login
  },
  {
    path:"/home",
    component: Home,
  },
  {
    path:"/order",
    component: Order
  },
  {
    path:"/shop",
    component: Shop
  },
  {
    path:"/follow",
    component: Follow
  },
  {
    path:"/personal",
    component: Personal
  },
  {
    path:"/shopManage",
    component: shopManage
  },
]

const router = new VueRouter({
  routes
})
const originalPush = VueRouter.prototype.push
   VueRouter.prototype.push = function push(location) {
   return originalPush.call(this, location).catch(err => err)
}

// 挂载路由导航守卫
router.beforeEach((to,from,next)=>{
  //to 将要访问
  // from 从哪访问
  // next 接着干 next（url） 重定向到url上  next（）继续访问to的路径
  if(to.path=='/login') return next();
  // 获取user
  const dbUserFlag = window.sessionStorage.getItem("dbUser"); // 取出当前用户
  if(!dbUserFlag) return next('/login'); // 无值 返回登录界面
  next();  // 符合要求 放行
})

export default router


